package com.balloon.mall.ocr;

import com.balloon.mall.aop.RequestSingleParam;
import com.balloon.mall.comm.ResultVO;
import com.balloon.mall.comm.ResultVOUtils;
import com.balloon.mall.minio.MinioUtil;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;

/**
 * PDF ocr识别
 * @author liaofuxing
 * @date 2022/08/26 14:22
 */
@RestController
public class OcrController {

    @Autowired
    private MinioUtil minioUtil;
    /**
     * 获取文件列表
     *
     * @return ResultVO<List<FileInfo>>
     */
    @ResponseBody
    @PostMapping(value = "/ocrPdf")
    public ResultVO<Object> getFileList(@RequestSingleParam(value = "fileName")String fileName) throws Exception {
        long startTime = System.currentTimeMillis();
        InputStream is = minioUtil.getObjectInputStream("css", fileName);
        PDDocument doc = PDDocument.load(is);

        PDFTextStripper text = new PDFTextStripper();
        //获取全文件的所有文本
        String finalText = text.getText(doc);

        System.out.println(finalText);
        System.out.println("耗时：" + (System.currentTimeMillis() - startTime) + "ms");
        //关闭
        doc.close();
        return ResultVOUtils.success(finalText);
    }
}
